﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;


using ZX.SYS.Bussiness.DesignForm;
using ZX.SYS.Library;
using ZX.DBModule;


namespace ZX.WebApp.DesignForm
{
    public partial class FormSave : ZX.Web.UI.Controls.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xFormID">表单ID</param>
        /// <param name="xFormName">表单名称</param>
        /// <param name="xMasterTableName">主表名称</param>
        /// <param name="xTemplateName">模板名称</param>
        /// <param name="xMasterJson">主表设置信息</param>
        /// <param name="xDetailsJson">明细设置信息</param>
        /// <param name="xHtmlString">html字符串</param>
        /// <param name="xVersion">版本</param>
        /// <param name="xFieldItems">字段数据源Json</param>
        /// <param name="isNew">是否是新记录</param>
        /// <returns></returns>
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string SaveForm(string xFormID, string xFormName, string xMasterTableName, string xTemplateName,
            string xMasterJson, string xDetailsJson, string xHtmlString,
            string xVersion, Dictionary<string, string>[] xFieldItems, string isNew)
        {
            string msg = "";
            try
            {
                SysCustomDefineForm form = new SysCustomDefineForm();
                if (isNew == "0")
                {
                    form.FormID = new Guid(xFormID);
                    form.Version = 0;
                    form.Patch = 1;
                }
                else
                {
                    Guid _FormID = new Guid(xFormID);
                    form = SysCustomDefineForm.Get(_FormID);
                    if (string.IsNullOrEmpty(xVersion))
                    {
                        form.Version = 0;
                    }
                    else
                    {
                        form.Version = Convert.ToInt32(xVersion);
                    }
                    form.Patch = form.Patch + 1;
                }
                form.FormName = xFormName;
                form.MasterTableName = xMasterTableName;
                form.TemplateName = xTemplateName;
                form.MasterJson = xMasterJson;
                form.DetailsJson = xDetailsJson;
                form.HtmlString = xHtmlString;
                ZX.EHR.Library.DatUser U = HttpContext.Current.Session["User"] as ZX.EHR.Library.DatUser;
                form.AddUser = U.UserID;//当前用户
                form.Addtime = DateTime.Now;
                form=importFieldItems(ref form, xFieldItems);
                new BSysCustomDefingForm().SaveForm(form);
                msg = "OK@" + form.FormID;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return msg;

        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string testSaveForm(Dictionary<string, string> []xFieldItems)
        {
            string msg = "";

         
            return msg;

        }


        private static SysCustomDefineForm importFieldItems(ref SysCustomDefineForm form, Dictionary<string, string>[] xFieldItems)
        {
            if (xFieldItems.Length > 0)
            {
                try
                {
                    form.FieldItems.Clear();//清空集合
                    foreach (Dictionary<string, string> item in xFieldItems)
                    {
                        //构造一个新的FieldItem对象。

                        SysFieldItem fieldItem = form.FieldItems.AddNew();
                        //为新构造的FieldItem对象属性赋值。
                        foreach (string key in item.Keys)
                        {
                            switch (key)
                            {
                                case "ID":
                                    if (!string.IsNullOrEmpty(item[key]))
                                    {
                                        fieldItem.ID = new Guid(item[key]);
                                    }
                                    else
                                    {
                                        fieldItem.ID = Guid.NewGuid();
                                    }

                                    break;
                                case "FormID":
                                    fieldItem.FormID = new Guid(item[key]);
                                    break;
                                case "TableName":
                                    fieldItem.TableName = item[key];
                                    break;
                                case "Caption":
                                    fieldItem.Caption = item[key];
                                    break;
                                case "FieldName":
                                    fieldItem.FieldName = item[key];
                                    break;
                                case "DefualtValue":
                                    fieldItem.DefualtValue = item[key];
                                    break;
                                case "DataSourceType":
                                    fieldItem.DataSourceType = item[key];
                                    break;
                                case "DataSource":
                                    fieldItem.DataSource = item[key];
                                    break;
                                case "IsRequire":
                                    if (!string.IsNullOrEmpty(item[key]))
                                    {
                                        fieldItem.IsRequire = Convert.ToInt32(item[key]);
                                    }
                                    else { fieldItem.IsRequire = 0; }
                                    break;
                                case "ControlsType":
                                    fieldItem.ControlsType = item[key];
                                    break;
                                default:
                                    break;
                            } 
                        } 
                    }
                }
                catch
                {
                    throw new Exception("导入字段数据源失败!");
                }
            }  
            return form;
        }
       
        //private static void UpdateFieldItem(ref SysCustomDefineForm form, SysFieldItem newItem)
        //{
        //    SysFieldItem item = null;
        //    foreach (SysFieldItem _item in form.FieldItems)
        //    {
        //        if (_item.ID == newItem.ID) 
        //        {
        //            item = _item;
        //        }
        //    }

        //    item.FormID = newItem.FormID;
        //    item.TableName = newItem.TableName;
        //    item.Caption = newItem.Caption;
        //    item.FieldName = newItem.FieldName;
        //    item.DefualtValue = newItem.DefualtValue;
        //    item.DataSourceType = newItem.DataSourceType;
        //    item.DataSource = newItem.DataSource;
        //    item.IsRequire = newItem.IsRequire; 
        
        //}
        //private static bool IsExistItem(SysCustomDefineForm form, Guid formID) 
        //{
        //    bool isExist = false;
        //    foreach (SysFieldItem _item in form.FieldItems)
        //    {
        //        if (_item.ID == formID) { isExist = true; continue; }
        //    }
        //    return isExist;
        //}

    }
}